package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.warning.domain.entity.AccessPoints;
import com.ruoyi.warning.domain.query.AccessPointQuery;
import com.ruoyi.warning.service.IAccessPointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-06-25
 */
@RestController
@RequestMapping("/accessPoints")
@Api(tags = "无线AP", description = "用于处理无线AP相关操作的控制器")
public class AccessPointsController {
    @Resource
    private IAccessPointsService accessPointsService;

    @ApiOperation("获取无线AP分页")
    @Log(title = "获取无线AP分页", businessType = BusinessType.SELECT)
    @GetMapping(value = "/getPage")
    public R getPage(AccessPointQuery query) {
        Page<AccessPoints> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<AccessPoints> list = accessPointsService.list(new QueryWrapper<AccessPoints>().lambda()
                .eq(query.getDeviceId()!=null,AccessPoints::getDeviceId,query.getDeviceId())
                .like(query.getName()!=null,AccessPoints::getName,query.getName())
                .likeRight(query.getInitialName()!=null,AccessPoints::getName,query.getInitialName())
                .eq(query.getHostname()!=null,AccessPoints::getHostname,query.getHostname())
                .eq(query.getApGroup()!=null,AccessPoints::getApGroup,query.getApGroup())
                .eq(query.getStatus()!=null&&query.getStatus()==1,AccessPoints::getStatus,1)
                .ne(query.getStatus()!=null&&query.getStatus()!=1,AccessPoints::getStatus,1)
                .orderByDesc(AccessPoints::getStatus));
        //规范 mac地址
        for (AccessPoints accessPoints : list) {
            String macAddr = accessPoints.getMacAddr();
            if (macAddr != null) {
                // 先将单字符补全为双字符
                String[] parts = macAddr.split(":");
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < parts.length; i++) {
                    if (parts[i].length() == 1) {
                        sb.append("0").append(parts[i]);
                    } else {
                        sb.append(parts[i]);
                    }
                    if (i < parts.length - 1) {
                        sb.append(":");
                    }
                }
                macAddr = sb.toString();
                accessPoints.setMacAddr(macAddr);
            }
            if(ObjectUtils.isEmpty(accessPoints.getStatus())){
                if((ObjectUtils.isNotEmpty(accessPoints.getUp())&&accessPoints.getUp()>0)
                || (ObjectUtils.isNotEmpty(accessPoints.getDown())&&accessPoints.getDown()>0)
                || (ObjectUtils.isNotEmpty(accessPoints.getNumasoclients()) && accessPoints.getNumasoclients()>0)
                || (ObjectUtils.isNotEmpty(accessPoints.getTxpow()) && accessPoints.getTxpow()>0)
                || (ObjectUtils.isNotEmpty(accessPoints.getChannel()) && accessPoints.getChannel()>0)
                || accessPoints.getRadioutil()>0){
                    accessPoints.setStatus(1);
                }
            }
            if ("dot11b".equals(accessPoints.getType())) {
                accessPoints.setType("2.4GHz");
            } else if ("dot11a".equals(accessPoints.getType())) {
                accessPoints.setType("5GHz");
            }
            if(ObjectUtils.isEmpty(accessPoints.getArea())){
                //截取名称前两个字符作为厂区
                accessPoints.setArea(accessPoints.getName().substring(0,2));
            }
        }
        TableDataInfo tableDataInfo = new TableDataInfo(list, page.getTotal());
        return R.ok(tableDataInfo);
    }

    @ApiOperation("刷新ap状态")
    @GetMapping(value = "/getAPStatus/{id}")
    public R refreshAPStatus(@PathVariable Integer id){
        accessPointsService.refreshAPStatus(id);
        return R.ok();
    }

    @ApiOperation("获取无线AP详情")
    @GetMapping(value = "/getAPDetail/{id}")
    public R getAPDetail(@PathVariable Integer id){
        return R.ok(accessPointsService.getById(id));
    }
}
